package com.ITD.service.Impl;

import com.ITD.mapper.EventMapper;
import com.ITD.mapper.EventTypeMapper;
import com.ITD.pojo.*;
import com.ITD.service.EventService;
import com.ITD.service.StudentService;
import com.ITD.vo.EventTypeListVO;
import com.ITD.vo.Result;
import com.ITD.vo.SumEventsVO;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EventServicrImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private EventTypeMapper eventTypeMapper;
    @Autowired
    private StudentService studentService;

    public static List<QueryResultParam> threadLocal =  new ArrayList<>();
    @Override
    public Result insertNewEvent(NewEvent newEvent,String authorization) {
        System.out.println("插入新事件");
        //todo：调用学生服务层方法完成分数的计算和录入和事件描述细则
        //组装数据
        //todo : 获取事件唯一id
        //获取学生id
        Event event = new Event();
        event.setStudentId(newEvent.getStuno());
        event.setWork_id(newEvent.getType_id());
        event.setStatus((short) 1);
        Date date = new Date();
        event.setCreate_time(date);
        //todo : 获取当前用户id
        int id=0;
        if(authorization!=null)id=123;
        event.setCreate_user_id(id);

//        //补充数据 为什么没有写更新数据也会创建一个数据：通过设置数据库字段属性完成不写时间也能默认自动插入当前日期数据
//        event.setUpdate_time(date);
//        event.setUpdate_user_id(id);


        //调用插入事件
        int insert = eventMapper.insert(event);
        if(insert==1)
            return Result.success("插入事件成功");
        else
            return Result.fail(40001,"插入事件失败","");
    }

    /**
     * 查询筛选事件
     * @param queryWorkParam
     * @param authorization
     * @return
     */
    @Override
    public Result queryEvents(QueryWorkParam queryWorkParam, String authorization){

        //联表查询，mp原生不支持
//        LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper
//                .eq(queryWorkParam.getEvent_id() != null, Event::getId, queryWorkParam.getEvent_id())  // 如果 name 不为空，则添加等于条件
//                // .eq(queryEventParam.getStudent_name() != null, Event::getAge, userQueryDTO.getAge())      // 如果 age 不为空
//                .eq(queryWorkParam.getStudent_no() != null, Event::getStudentId, queryWorkParam.getStudent_no())// 如果 email 不为空
//                .eq(Event::get, student.getStudent_id())   // 如果 city 不为空
//                .eq(queryWorkParam.getCreate_user_name() != null, Student::getPhoneNumber, userQueryDTO.getPhoneNumber()) // 如果 phoneNumber 不为空
//                .eq(queryWorkParam.getType_id() != null, Event::getWork_id, queryWorkParam.getType_id()); // 如果 phoneNumber 不为空

        List<QueryResultParam> list=eventMapper.queryEvents(queryWorkParam.getHappen_date_begin(),queryWorkParam.getHappen_date_end(),
                queryWorkParam.getClass_id(),queryWorkParam.getDetail(),queryWorkParam.getStudent_name(),
                queryWorkParam.getStudent_no(),queryWorkParam.getEvent_id(),queryWorkParam.getCreate_user_name());

        threadLocal=list;
        return Result.success(list);
    }
    @Override
    public Result sumEvents(QuerySumParam querySumParam) {
        //获取事件名称
        String name= eventTypeMapper.selectById(querySumParam.getType_id()).getName();
        //查询输入事件pid
        LambdaQueryWrapper<EventType> lqw=new LambdaQueryWrapper<>();
        lqw.eq(EventType::getId,querySumParam.getType_id());
        int pid = eventTypeMapper.selectOne(lqw).getPid();//结果集获取成员变量！！！lqw不能直接返回到成员变量

        //判断pid是否为0：为0则为一级往下找二级
        //从上往下找：如果是二级列表直接找相应的数据，如果是一级列表才要找二级列表
        List<Integer> ids=new ArrayList<>();
        ids.add(querySumParam.getType_id());
        if(pid==0){
            LambdaQueryWrapper<EventType> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(EventType::getPid,querySumParam.getType_id());
            List<EventType> eventTypes = eventTypeMapper.selectList(lqw2);
            for(EventType eventType : eventTypes){
                ids.add(eventType.getId());
            }
        }

        List<SumEvents> list=new ArrayList<>();
        for(Integer id:ids) {
            list.addAll( eventMapper.sumEvents(id, querySumParam.getClass_id(),
                querySumParam.getHappen_date_begin(), querySumParam.getHappen_date_end()));
        }
        int total=list.size();

//        String id =querySumParam.getType_id();
//        eventTypeMapper.selectById(id);
//        String name=eventTypeMapper.selectById(id).getName();


        SumEventsVO sumEventsVo = new SumEventsVO(name,total,list);

        return Result.success(sumEventsVo);
    }

    @Override
    public Result getEventType() {
        LambdaQueryWrapper<EventType> lqw=new LambdaQueryWrapper<>();
        lqw.eq(EventType::getGradeId,2);
        List<EventType> list = eventTypeMapper.selectList(lqw);
        EventTypeListVO eventTypeListVO=new EventTypeListVO(list.size(),list);
        return Result.success(eventTypeListVO);
    }

    @Override
    public Result check(String nums) {
        String[] numList = nums.split("\\|");
        return Result.success(studentService.getNameList(numList));
    }
    public void exportExcel (){
        List<QueryResultParam> list= threadLocal;
        String fileName = "事件表.xlsx";
        EasyExcel.write(fileName, QueryResultParam.class).sheet("学生事件信息").doWrite(list);
        System.out.println("导出成功");
    }
}
